package custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.qianfeng.lingshijia.R;
public class NaviAnimView extends FrameLayout {
	
	private int count;
	private LinearLayout uncheckLayout;
	private LinearLayout checkedLayout;
	private int checkedImage;
	private int uncheckImage;
	public NaviAnimView(Context context, AttributeSet attrs) {
		super(context, attrs);
		parserAttr(attrs);
		init();
	}

	private void parserAttr(AttributeSet attrs) {
		TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.nav);
		checkedImage = typedArray.getResourceId(R.styleable.nav_checkedImg, 0);
		uncheckImage = typedArray.getResourceId(R.styleable.nav_uncheckImg, 0);
		typedArray.recycle();
	}

	public NaviAnimView(Context context) {
		super(context);
		init();
	}

	private void init() {
		LayoutInflater.from(getContext()).inflate(R.layout.custem_navianimview, this, true);
		uncheckLayout = (LinearLayout) findViewById(R.id.navi_uncheck);
		checkedLayout = (LinearLayout) findViewById(R.id.navi_checked);
	}

	public void setCount(int count){
		this.count = count;
		initView();
	}

	private void initView() {
		if (count>0) {
			for (int i = 0; i < count; i++) {
				ImageView imageView = new ImageView(getContext());
				imageView.setImageResource(uncheckImage);
				uncheckLayout.addView(imageView, i);
			}
			ImageView imageView = new ImageView(getContext());
			imageView.setImageResource(checkedImage);
			checkedLayout.addView(imageView);
		}
	}
	
	public void setIndexAnim(int position, float positionOffset){
		ImageView iv = (ImageView) checkedLayout.getChildAt(0);
		LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) iv.getLayoutParams();
		layoutParams.leftMargin = (int) (iv.getWidth()*(position + positionOffset));
		iv.setLayoutParams(layoutParams);
	}
	
	
}
